var prefix = jsctx + "/tenant/user";

var modifyPwd = (function () {
    function init() {
        validateRule();
    };

    //获取验证码
    function getAuthCode() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/genModifyPwdAuthCode",
            data: $('#frmPassword').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("验证码已发送");
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    };

    function save() {
        var data = $('#frmPassword').serialize();

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/saveNewPw",
            data: $('#frmPassword').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    // var index = parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);
                    modifyPwd.goBack();
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    };

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmPassword").validate({
            rules: {
                oldPw: {
                    required: true
                },
                newPw: {
                    required: true,
                    minlength: 6
                },
                repNewPw: {
                    required: true,
                    minlength: 6,
                    equalTo: "#newPw"
                },
                authCode: {
                    required: true
                }
            },
            messages: {
                oldPw: {
                    required: icon + "请输入原来密码"
                },
                newPw: {
                    required: icon + "请输入新密码",
                    minlength: "密码长度不能小于 6 个字母"
                },
                repNewPw: {
                    required: icon + "请确认新密码",
                    minlength: "密码长度不能小于 6 个字母",
                    equalTo: "两次密码输入不一致"
                },
                // authCode: {
                //     required: icon + "请输入验证码"
                // }
            },
            submitHandler: function (form) {
                save();
            }
        })
    };

    function goBack() {
        location.href = jsctx + "/basic";
    }

    return {
        init: init,
        goBack: goBack,
        getAuthCode: getAuthCode
    };
})();